home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Demos / Bowers Development / AppMaker 2.1.sit / AppMaker 2.1 / Examples / Procedural / Gadgets / MainMenu.c / MainMenu.c
Encoding:
C/C++ Source or Header  |  1997-06-10  |  3.9 KB  |  206 lines  |  [TEXT/CWIE]

  1. /* MainMenu.c */
  2. /* Created 6/10/97 12:24 PM by AppMaker */
  3.  
  4. #include <Types.h>
  5. #include <Quickdraw.h>
  6. #include <Controls.h>
  7. #include <Dialogs.h>
  8. #include <Events.h>
  9. #include <Lists.h>
  10. #include <LowMem.h>
  11. #include <Menus.h>
  12. #include <TextEdit.h>
  13. #include "Globals.h"
  14. #include "ResourceDefs.h"
  15. #include "Miscellany.h"
  16.  
  17. #include "AboutDialog.h"
  18. #include "FileM.h"
  19. #include "EditM.h"
  20.  
  21. #include <ToolUtils.h>
  22. #include "MainMenu.h"
  23.  
  24.  
  25. static void Enable            (short            itemNr,
  26.                              Boolean        enabled);
  27. static void EnableTitle        (MenuHandle        menu,
  28.                              Boolean        enabled);
  29. static void DoApple            (short            itemNr);
  30.  
  31. /*----------*/
  32. void InitTitles (void)
  33. {
  34.     InitFileM ();
  35.     InitEditM ();
  36.  
  37. } /*InitTitles*/
  38.  
  39. /*----------*/
  40. void LoadMenus (void)
  41. {
  42.     AppleMenu    = GetMenu (MENU_Apple);
  43.     FailNilResource ((Handle)AppleMenu);
  44.     AppendResMenu (AppleMenu, 'DRVR');
  45.     FileMenu    = GetMenu (MENU_File);
  46.     EditMenu    = GetMenu (MENU_Edit);
  47.  
  48.     InsertMenu (AppleMenu, 0);
  49.     InsertMenu (FileMenu, 0);
  50.     InsertMenu (EditMenu, 0);
  51.  
  52.     DrawMenuBar ();
  53. } /*LoadMenus*/
  54.  
  55. /*----------*/
  56. static void DoAbout (void)
  57. {
  58.     AboutDialogRec                AboutDialogInfo;
  59.     register AboutDialogRecPtr    info;
  60.  
  61.     info = &AboutDialogInfo;
  62.     /*initialize AboutDialog info*/
  63.  
  64.     if (GetAboutDialog (&AboutDialogInfo)) {
  65.         /*use AboutDialog info*/
  66.     }
  67. } /*DoAbout*/
  68.  
  69.  
  70. /*----------*/
  71. static void DoApple (short     itemNr)
  72. {
  73.     Str255            name;
  74.     short            refNum;
  75.  
  76.     switch (itemNr) {
  77.         case cAppleAbout:
  78.                 DoAbout ();
  79.             break;
  80.  
  81.         default:
  82.                 GetMenuItemText (AppleMenu, itemNr, name);
  83.                 refNum = OpenDeskAcc (name);
  84.             break;
  85.     } /*switch*/
  86. } /*DoApple*/
  87.  
  88. /*----------*/
  89. void DoMenu (long        menuChoice)
  90. {
  91.     short            menuID;
  92.     short            itemNr;
  93.  
  94.     menuID = HiWord (menuChoice);
  95.     itemNr = LoWord (menuChoice);
  96.  
  97.     switch (menuID) {
  98.     case 0:
  99.             /*Do nothing*/;
  100.         break;
  101.     case MENU_Apple:
  102.             DoApple (itemNr);
  103.         break;
  104.     case MENU_File:
  105.             DoFile (itemNr);
  106.         break;
  107.     case MENU_Edit:
  108.             DoEdit (itemNr);
  109.         break;
  110.  
  111.     } /*switch*/
  112.  
  113.     HiliteMenu (0);
  114. } /*DoMenu*/
  115.  
  116. /*----------*/
  117. MenuHandle        menu;
  118. Boolean            menuBarChanged;
  119.  
  120. /*----------*/
  121. static void Enable    (short        itemNr,
  122.                      Boolean    enabled)
  123. {
  124.     if (enabled) {
  125.         EnableItem  (menu, itemNr);
  126.     } else {
  127.         DisableItem (menu, itemNr);
  128.     }
  129. } /*Enable*/
  130.  
  131. /*----------*/
  132. static void EnableTitle    (MenuHandle        menu,
  133.                          Boolean        enabled)
  134. {
  135.     if (enabled != ((**menu).enableFlags & 1)) {
  136.         menuBarChanged = true;
  137.     }
  138.     if (enabled) {
  139.         EnableItem  (menu, 0);
  140.     } else {
  141.         DisableItem (menu, 0);
  142.     }
  143. } /*EnableTitle*/
  144.  
  145. /*----------*/
  146. void UpdateMenus (void)
  147. {
  148.     WindowPeek        frontPeek;
  149.     Boolean            isFront;            /*is there a front window?*/
  150.     Boolean            isCur;                /*is there a current window?*/
  151.     Boolean            isDirty;            /*is it dirty?*/
  152.     Boolean            hasFile;            /*does it have a file?*/
  153.     Boolean            isSelected;            /*is anything selected?*/
  154.     Boolean            isDesk;                /*is the front window a desk acc?*/
  155.     Boolean            isText;                /*is there a current text field?*/
  156.     Boolean            isScrap;            /*is there any scrap?*/
  157.     menuBarChanged = false;
  158.  
  159.     isFront        = (FrontWindow () != nil);
  160.     isCur        = (curWindow != nil);
  161.     isDirty        = false;
  162.     hasFile        = false;
  163.     isSelected    = false;
  164.     if (isCur) {
  165.         isDirty        =  cur->dirty;
  166.         hasFile        = (cur->fileNum != 0);
  167.     }
  168.  
  169.     isDesk = false;
  170.     if (isFront) {
  171.         frontPeek    = (WindowPeek) FrontWindow ();
  172.         isDesk        = (frontPeek->windowKind < 0);
  173.     }
  174.     isText        = isCur && (cur->text != nil);
  175.     isScrap        = false;
  176.     if (isText) {
  177.         isSelected    = ((**(cur->text)).selStart != (**(cur->text)).selEnd);
  178.         isScrap        = (TEGetScrapLength () > 0);
  179.     }
  180.  
  181.     menu = FileMenu;
  182.     Enable (cFileClose,        isFront);
  183.     Enable (cFileSave,        isDirty);
  184.     Enable (cFileSaveAs,        isCur);
  185.     Enable (cFileRevert,        isDirty);
  186.  
  187.     menu = EditMenu;
  188.     if (isFront) {
  189.         Enable (cEditUndo,        isDesk);
  190.         Enable (cEditCut,        isDesk || isSelected);
  191.         Enable (cEditCopy,        isDesk || isSelected);
  192.         Enable (cEditPaste,        isDesk || isScrap);
  193.         Enable (cEditClear,        isDesk || isSelected);
  194.         Enable (cEditSelectAll,    isText);
  195.  
  196.     }
  197.     EnableTitle (EditMenu,     isFront);
  198.  
  199.  
  200.     if (menuBarChanged) {
  201.         DrawMenuBar ();
  202.     }
  203. } /*UpdateMenus*/
  204.  
  205. /* MainMenu */
  206.